#pragma once

#include <string>
#include <iostream>
#include <jsoncpp/json/json.h>

// #define Myself=1

const std::string blank_space_sep = " ";
const std::string protocol_sep = "\n";

std::string Encode(std::string &contect)
{
    std::string package = std::to_string(contect.size());
    package += protocol_sep;
    package += contect;
    package += protocol_sep;

    return package;
}

bool Decode(std::string &package, std::string *contect)
{
    std::size_t pos = package.find(protocol_sep);
    if (pos == std::string::npos)
        return false;
    std::string len_str = package.substr(0, pos);
    std::size_t len = std::stoi(len_str);
    std::size_t total_len = len + len_str.size() + 2;
    if (package.size() < total_len)
        return false;

    *contect = package.substr(pos + 1, len);

    // 移除报文
    package.erase(0, total_len);

    return true;
}

class Request
{
public:
    Request(int data1, int data2, char op)
        : _x(data1), _y(data2), _op(op)
    {
    }
    Request()
    {
    }

    bool Serialize(std::string *out)
    {
#ifdef Myself
        // 构建有效载荷
        std::string s = std::to_string(_x);
        s += blank_space_sep;
        s += _op;
        s += blank_space_sep;
        s += std::to_string(_y);
        *out = s;

        return true;
#else
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["op"] = _op;

        Json::StyledWriter w;
        *out = w.write(root);

        return true;
#endif
    }

    bool DeSerialize(const std::string &in)
    {
#ifdef Myself
        std::size_t left = in.find(blank_space_sep);
        if (left == std::string::npos)
            return false;
        std::string part_x = in.substr(0, left);

        std::size_t right = in.rfind(blank_space_sep);
        if (right == std::string::npos)
            return false;
        std::string part_y = in.substr(right + 1);

        if (left + 2 != right)
            return false;

        _op = in[left + 1];
        _x = std::stoi(part_x);
        _y = std::stoi(part_y);

        return true;
#else
        Json::Value v;
        Json::Reader r;
        r.parse(in, v);

        _x = v["x"].asInt();
        _y = v["y"].asInt();
        _op = v["op"].asInt();

        return true;
#endif
    }

    void DebugPrint()
    {
        std::cout << "新请求构建完成:  " << _x << _op << _y << "=?" << std::endl;
    }

public:
    int _x;
    int _y;
    char _op;
};

class Respond
{
public:
    Respond(int ret, int c)
        : _result(ret), _code(c)
    {
    }
    Respond()
    {
    }

    bool Serialize(std::string *out)
    {
#ifdef Myself
        std::string s = std::to_string(_result);
        s += blank_space_sep;
        s += std::to_string(_code);
        *out = s;

        return true;
#else
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;

        Json::StyledWriter w;
        *out = w.write(root);

        return true;
#endif
    }

    bool DeSerialize(const std::string &in)
    {
#ifdef Myself
        std::size_t pos = in.find(blank_space_sep);
        if (pos == std::string::npos)
            return false;
        std::string part_left = in.substr(0, pos);
        std::string part_right = in.substr(pos + 1);

        _result = std::stoi(part_left);
        _code = std::stoi(part_right);

        return true;
#else
        Json::Value v;
        Json::Reader r;
        r.parse(in, v);
        _result = v["result"].asInt();
        _code = v["code"].asInt();

        return true;
#endif
    }

    void DebugPrint()
    {
        std::cout << "结果响应完成, result: " << _result << ", code: " << _code << std::endl;
    }

public:
    int _result;
    int _code;
};